﻿using MediatR;
using Yuekao.API.Read.Command;
using Yuekao.Domain.API;
using Yuekao.Domain.BaseReposi;
using Yuekao.Domain.Entitys;

namespace Yuekao.API.Read.CommandHandler
{
    public class WorkflowDetinitionShowCommandHandler : IRequestHandler<WorkflowDetinitionShowCommand, APIResult<APIPagging<WorkflowDefinitionModel>>>
    {
        private readonly IBaseRepository<WorkflowDefinitionModel> _repository;  
        public WorkflowDetinitionShowCommandHandler(IBaseRepository<WorkflowDefinitionModel> repository)
        {
            _repository= repository;
        }
        public Task<APIResult<APIPagging<WorkflowDefinitionModel>>> Handle(WorkflowDetinitionShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPagging<WorkflowDefinitionModel>> result = new APIResult<APIPagging<WorkflowDefinitionModel>>();
            result.Code=APIEnum.Success;
            result.Message = "流程设计列表显示成功";
            var list = _repository.GetAll();
            var totaCount=list.Count();
            var pageCount = (int)Math.Ceiling(totaCount*1.0/request.pageSize);
            APIPagging<WorkflowDefinitionModel> pagging=new APIPagging<WorkflowDefinitionModel>();
            pagging.TotaCount=totaCount;
            pagging.PageCount=pageCount;
            pagging.PageData =list.OrderByDescending(x=>x.CreateTime).Skip((request.pageIndex-1) * request.pageSize).Take(request.pageSize).ToList();
            result.Data=pagging;
            return Task.FromResult(result);
        }
    }
}
